package com.qingfeng.client.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingfeng.client.DO.ShoppingCartStoreDo;
import com.qingfeng.client.bean.Goods;
import com.qingfeng.client.bean.ShoppingCart;
import com.qingfeng.client.dao.GoodsMapper;
import com.qingfeng.client.dao.ShoppingCartMapper;
import com.qingfeng.client.util.Page;
import com.qingfeng.exception.LsException;
import com.qingfeng.util.CodeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.ls.LSException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ShoppingCartService {

    @Resource
    private GoodsMapper goodsDao;

    @Resource
    private ShoppingCartMapper shoppingCartDao;

    /**
     * 将cookie中的购物车信息添加到数据库中
     * @param request
     * @param id 用户id
     */
    public void saveCookieToDb(HttpServletRequest request, Long id) {
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    public boolean add(ShoppingCart shoppingCart) {
        Goods goods = goodsDao.selectById(shoppingCart.getGoodsId());
        if (goods.getState()!=1){
            throw new LsException(CodeEnum.ERROR,"该商品已下架");
        }
        if (goods.getCanSellSize()<=0){
            throw new LsException(CodeEnum.ERROR,"库存不足");
        }
        List<ShoppingCart> list =
                shoppingCartDao.selectByBuyerIdAndGoodsId(shoppingCart.getBuyerId(),shoppingCart.getGoodsId());
        if (list.size()>0){
            ShoppingCart shoppingCart1 = list.get(0);
            int newNum = shoppingCart1.getNum() + shoppingCart.getNum();

            if (goods.getCanSellSize()<newNum){
                throw new LsException(CodeEnum.ERROR,"库存不足");
            }
            shoppingCart1.setNum(newNum);
            shoppingCartDao.updateById(shoppingCart1);
        }else {
            if (goods.getCanSellSize()<shoppingCart.getNum()){
                throw new LsException(CodeEnum.ERROR,"库存不足");
            }
            //没有该数据,直接存入购物车shoppingCart
            shoppingCartDao.insert(shoppingCart);
        }
        return true;
    }

    /**
     * 根据商品id删除购物车
     * @param goodsIds
     * @param buyerId
     */
    public void delete(String goodsIds, Long buyerId) {
        shoppingCartDao.deleteByBuyerIdAndIds(buyerId,goodsIds);
    }

    public void addNum(int num, long shopCartId) throws LsException {
        ShoppingCart shoppingCart = shoppingCartDao.selectById(shopCartId);
        if (shoppingCartDao==null){
            throw new LsException(500,"购物车中不存在该商品");
        }
        Goods goods = goodsDao.selectById(shoppingCart.getGoodsId());
        if (goods==null){
            throw new LsException(CodeEnum.ERROR,"该商品消失了");
        }
        if (num > goods.getCanSellSize()) {
            throw new LsException(CodeEnum.ERROR,"库存不足");
        }
        if (num<0){
            throw new LsException(CodeEnum.ERROR,"数量不为负数");
        }
        shoppingCart.setNum(num);
        shoppingCartDao.updateById(shoppingCart);
    }

    public IPage<ShoppingCartStoreDo> getByBuyerId(Page page, Long buyerId) {
        return shoppingCartDao.getShopCatGoodsDoByBuyer(page,buyerId);
    }
}
